const shareData = require('../model/shares.json');

/** @method 获取所有用户的分享 */
const getShareData = {
	url: '/getShareData',
	type: 'get',
	response: () => {
		// 随机返回20个
		let random = Math.floor(Math.random() * 20);
		return shareData.slice(random, random + 20);
	},
};

/**
 * @method 获取单条用户的分享
 * @param {String} id id
 *  */
const getShareDataById = {
	url: '/getShareDataById',
	type: 'get',
	response: (req) => {
		const { id } = JSON.parse(req.query);
		const share = shareData.find((item) => item.id == id);
		if (!share) {
			return {
				code: 500,
				msg: '获取不到分享的信息',
			};
		}
		return share;
	},
};

/**
 * @method 随机获取分享数据
 * @param {number} count 获取的数据条数
 *  */
const getRandomData = {
	url: '/getRandomData',
	type: 'get',
	response: (req) => {
		const count = req.query;
		// 随机获取四条数据
		const randomData = [];
		for (let i = 0; i < count; i++) {
			const randomIndex = Math.floor(Math.random() * shareData.length);
			randomData.push(shareData[randomIndex]);
		}
		return randomData;
	},
};

export default [getShareData, getShareDataById, getRandomData];
